package com.jkt.rms.businessservice.impl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Property;
import org.springframework.beans.BeanUtils;

import com.jkt.rms.business.Project;
import com.jkt.rms.business.ProjectAuditTrans;
import com.jkt.rms.businessservice.ProjectBusinessService;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.dataservice.ProjectAuditTransDataService;
import com.jkt.rms.dataservice.ProjectDataService;
import com.jkt.rms.vo.ProjectVO;

public class ProjectBusinessServiceImpl implements ProjectBusinessService {
	private ProjectDataService projectDataService;
	private ProjectAuditTransDataService projectAuditTransDataService;

	public void setProjectDataService(ProjectDataService projectDataService) {
		this.projectDataService = projectDataService;
	}

	public void setProjectAuditTransDataService(ProjectAuditTransDataService projectAuditTransDataService) {
		this.projectAuditTransDataService = projectAuditTransDataService;
	}
	
	@Override
	public ProjectVO saveProject(ProjectVO projectVO) throws RMSException  {

		projectVO.setErrorMessage("");
		projectVO.setInfoMessage("");
		projectVO.setStatus('Y');

		Project project = new Project();

		project.setcreatedBy("admin");
		project.setcreatedDate(new Timestamp(System.currentTimeMillis()));

		BeanUtils.copyProperties(projectVO, project);

		project = projectDataService.saveProject(project);
		projectVO.setId(project.getId());
		projectVO.setInfoMessage("Data saved successfully.");
		return projectVO;
	}

	public List<Project> findByCriteria(DetachedCriteria crit, int offset, int maxResults) {
		return projectDataService.findByCriteria(crit, offset, maxResults);

	}

	// search projects
	public List<Project> searchProjects(ProjectVO projectVO) {
		
		/* search project on the basis of following properties if available
		 * ProjectNo, ProjectName, ProjectType, LineOfBusiness, Active/Inactive
		 */

		DetachedCriteria criteria = DetachedCriteria.forClass(Project.class);
		
		if (projectVO != null) {
			if (projectVO.getProjectNumber() != null && !projectVO.getProjectNumber().trim().equals("")) {
				criteria.add(Property.forName("projectNumber").like(projectVO.getProjectNumber(), MatchMode.ANYWHERE));
			}

			if (projectVO.getProjectName() != null && !projectVO.getProjectName().trim().equals("")) {
				criteria.add(Property.forName("projectName").like(projectVO.getProjectName(), MatchMode.ANYWHERE));
			}

			if (projectVO.getProjectType() != null && !projectVO.getProjectType().trim().equals("")) {
				criteria.add(Property.forName("projectType").like(projectVO.getProjectType(), MatchMode.ANYWHERE));
			}

			if (projectVO.getLineOfBusiness() != null && !projectVO.getLineOfBusiness().trim().equals("")) {
				criteria.add(Property.forName("lineOfBusiness").like(projectVO.getLineOfBusiness(), MatchMode.ANYWHERE));
			}
			if (projectVO.getStatus() != 'S' && projectVO.getStatus() != 'A') {
				criteria.add(Property.forName("status").eq(projectVO.getStatus()));
			}
		}
		
		List<Project> list = projectDataService.findByCriteria(criteria);
		return list;
	}
	
	public Project findProjectById(int projectId){
		return projectDataService.findProjectById(projectId) ;
	}
	
	public List<Project> findByCriteria(DetachedCriteria crit){
		return projectDataService.findByCriteria(crit);
	}
	public void updateProject(ProjectVO projectVO) throws RMSException{
		
		Project project = projectDataService.findProjectById(projectVO.getId());
		ProjectAuditTrans auditTrans = new ProjectAuditTrans();
		
		BeanUtils.copyProperties(project, auditTrans);
		
		auditTrans.setProjectId(project.getId());
		auditTrans.setAuditDate(new Timestamp(System.currentTimeMillis()));
		auditTrans.setUpdatedBy("Admin");
		projectAuditTransDataService.saveProjectAuditTrans(auditTrans);
		
		BeanUtils.copyProperties(projectVO, project);
		project.setUpdatedBy("Admin");
		project.setUpdatedDate(new Timestamp(System.currentTimeMillis()));
		projectDataService.updateProject(project);
	}
}
